package com.lipiao.makerandroid.View.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReadActivity extends AppCompatActivity {
    static final String TAG = "ReadActivity";
    String userNumber;

    TextView textViewDescribe;
    TextView textViewAuthor;
    Integer readId;//文章id
    String userNo;//文章作者
    String text;//文章信息
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_main);
        Intent i = getIntent();
        readId = i.getIntExtra("readId",0);

        userNo = i.getStringExtra("userNo");
        text = i.getStringExtra("text");
        userNumber = i.getStringExtra("userNumber");
//        System.out.println(text);
        textViewDescribe = findViewById(R.id.tv_project_article_describe);
        textViewAuthor = findViewById(R.id.tv_project_article_author);
        textViewDescribe.setText("\u3000\u3000"+text);
        textViewAuthor.setText("来自用户:"+userNo);
    }

    public void addCollection(View view){
        Call<ResponseBody> call = HttpUtil.getUserService().addCollection(userNumber,readId);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String strBack = null;
                try {
                    strBack = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(strBack.equals("collectSuccess")){
                    Toast.makeText(getBaseContext(),"收藏成功！",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getBaseContext(),"该文章已收藏！",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}
